package com.ajie.bluetoothcard.adapter;

import java.util.List;

import com.ajie.bluetoothcard.ui.MainBackCardActivity;
import com.ajie.bluetoothcard.ui.MainCardBoxActivity;
import com.ajie.bluetoothcard.ui.MainMyCardActivity;
import com.ajie.bluetoothcard.ui.MainProductInfoActivity;
import com.ajie.bluetoothcard.ui.R;


import android.app.Activity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


/**
 * @author liujie
 *  @Date: 2013-12-04
 * @version 1.0.0
 */
public class ViewPagerAdapter extends PagerAdapter {

	// 界面列表
	private List<View> views;
	private Activity activity;


	public ViewPagerAdapter(List<View> views, Activity activity) {
		this.views = views;
		this.activity = activity;
	}

	// 销毁arg1位置的界面
	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView(views.get(arg1));
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	// 获得当前界面数
	@Override
	public int getCount() {
		if (views != null) {
			return views.size();
		}
		return 0;
	}

	// 初始化arg1位置的界面
	@Override
	public Object instantiateItem(View arg0, int arg1) {
		((ViewPager) arg0).addView(views.get(arg1), 0);
		switch (arg1) {
		case 0:
			Button main_my_card = (Button) arg0.findViewById(R.id.main_my_card);
			main_my_card.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					goActivity(0);
				}

			});
			break;
		case 1:
			Button main_card_box = (Button) arg0.findViewById(R.id.main_card_box);
			main_card_box.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					goActivity(1);
				}
				
			});	
			break;
		case 2:
			Button main_back_card = (Button) arg0.findViewById(R.id.main_back_card);
			main_back_card.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					goActivity(2);
				}
				
			});
			break;
		case 3:
			Button main_product_info = (Button) arg0.findViewById(R.id.main_product_info);
			main_product_info.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					goActivity(3);
				}
				
			});
			break;

		default:
			break;
		}
		return views.get(arg1);
	}

	private void goActivity(int i) {
		Intent intent = null;
		if(i == 0){
			intent = new Intent(activity, MainMyCardActivity.class).putExtra("fromMain", true);
		}else if(i == 1){
			intent = new Intent(activity, MainCardBoxActivity.class);
		}else if(i == 2){
			intent = new Intent(activity, MainBackCardActivity.class);
		}else if(i == 3){
			intent = new Intent(activity, MainProductInfoActivity.class);
		}
		activity.startActivity(intent);
	}
	


	// 判断是否由对象生成界面
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return (arg0 == arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

}
